home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / wterm.arc / WTERMF.C < prev    next >
C/C++ Source or Header  |  1985-11-20  |  24KB  |  787 lines

  1. /*
  2.  * WTERMF - Window Terminal, 'full' version
  3.  *
  4.  * Matt Kimmel, 1987
  5.  *
  6.  * This program implements a terminal program inside a window as a desk
  7.  * accessory.  See the header in the 'stripped' version for programming
  8.  * notes.
  9.  *
  10.  * This version is larger and slower but contains many features - three
  11.  * font sizes, RS232 buffer sizing, a cursor, setting of RS232 parameters,
  12.  * and saving of configuration.  This version requires a resource file,
  13.  * WTERMF.C.
  14.  *
  15.  */
  16. #include <stdio.h>
  17. #include <gemdefs.h>
  18. #include <osbind.h>
  19. #include <obdefs.h>
  20. #include <string.h>
  21. #include "wtermf.h" /* Resource defs */
  22.  
  23. /* Defines for window gadgets */
  24. #define NAME 0x0001
  25. #define CLOSE 0x0002
  26. #define FULL 0x0004
  27. #define MOVE 0x0008
  28. #define SIZE 0x0020
  29. /* Macros to turn mouse pointer on and off */
  30. #define MOUSE_ON graf_mouse(257,&j)
  31. #define MOUSE_OFF graf_mouse(256,&j)
  32.  
  33. /* Structure for iorec() */
  34. typedef struct {
  35.   char *ibuf;
  36.   int ibufsiz;
  37.   int ibufhd;
  38.   int ibuftl;
  39.   int ibuflow;
  40.   int ibufhi;
  41.   } IORECORD;
  42.  
  43. /* VDI variables - why aren't these in a header file? */
  44. int contrl[12], intin[128],ptsin[128],intout[128],ptsout[128];
  45. int w_handle, handle, workin[11], workout[57];
  46. int x,y,w,h; /* Current coordinates of window */
  47. int j;
  48. int nx, ny; /* coordinates of next character to be output */
  49. int tx, ty, tw, th; /* Work area of window */
  50. int hsize, vsize; /* Horizontal and vertical size of characters */
  51. int apid, menuid;
  52. OBJECT *mainmenu, *setrs, *setfont, *rsize;
  53. int baud, ucr, flow, cursor, point, buffer_size; /* Various parameters */
  54. char *rb; /* Pointer to our RS232 buffer */
  55. char strsiz[10];
  56. char aboutstr[] = "[1][Window Term 1.0|Full Version|by Matt Kimmel, 1987][OK]";
  57.  
  58. main()
  59. {
  60.   int j;
  61.   int msg[8];
  62.   FILE *conf;
  63.   int dx,dy,dw,dh; /* Desktop work area */
  64.  
  65.   apid = appl_init();
  66.   if (!rsrc_load("WTERMF.RSC"))
  67.     for(;;)
  68.       evnt_mesag(msg); /* If we can't open the resource file, we'll
  69.                           just sit here and release the system to
  70.                           the time slicer */
  71.   rsrc_gaddr(0,MENU,&mainmenu);
  72.   rsrc_gaddr(0,RS232,&setrs);
  73.   rsrc_gaddr(0,FONT,&setfont);
  74.   rsrc_gaddr(0,BUFSET,&rsize);
  75.   ((TEDINFO *)rsize[BSIZ].ob_spec)->te_ptext = strsiz;
  76.   if ((conf = fopen("\WTERMF.INF","r")) == NULL) {
  77.     /* If we can't open the config file, set up the defaults */
  78.     baud = 7; /* 1200 baud */
  79.     ucr = 136; /* 8,N,1 */
  80.     flow = 0; /* no flow control */
  81.     cursor = 1; /* cursor on */
  82.     point = ((Getrez() == 2)?(10):(9)); /* normal font for this resolution */
  83.     buffer_size = 8192; /* 8K RS232 buffer */
  84.     /* default window coordinates */
  85.     x=50;
  86.     y=50;
  87.     w=200;
  88.     h=100;
  89.     }
  90.   else {
  91.     /* Get parameters from the config file */
  92.     fscanf(conf,"%d",&baud);
  93.     fscanf(conf,"%d",&ucr);
  94.     fscanf(conf,"%d",&flow);
  95.     fscanf(conf,"%d",&cursor);
  96.     fscanf(conf,"%d",&point);
  97.     fscanf(conf,"%d",&buffer_size);
  98.     fscanf(conf,"%d",&x);
  99.     fscanf(conf,"%d",&y);
  100.     fscanf(conf,"%d",&w);
  101.     fscanf(conf,"%d",&h);
  102.     fclose(conf);
  103.     }
  104.   wind_get(0,WF_WORKXYWH,&dx,&dy,&dw,&dh); /* Get work area of desktop */
  105.   /* Fix window coordinates if necessary to fit inside desktop */
  106.   if ((x < dx) || (x > (dx + dw))) x=dx;
  107.   if ((y < dy) || (y > (dy+dh))) y=dy;
  108.   if ((x + w) > (dx + dw)) w=((dx + dw) - x) - 8;
  109.   if ((y + h) > (dy + dh)) h=((dy + dh) - x);
  110.   /* Align window to be byte-aligned in screen RAM */
  111.   align8(&x,&y,&w,&h);
  112.   init_buttons(); /* Set up buttons in dialog boxes */
  113.   if ((point == 10) && (Getrez() != 2)) point = 9; /* Fix font for color */
  114.   Rsconf(baud,flow,ucr,-1,-1,-1);
  115.   menuid = menu_register(apid,"  Window Terminal");
  116.   handle = graf_handle(&j,&j,&j,&j);
  117.   for(j=0;j++<=9;workin[j]=1);
  118.   workin[10] = 2;
  119.   v_opnvwk(workin,&handle,workout);
  120.   vst_alignment(handle,0,5,&j,&j);
  121.   vst_point(handle,point,&j,&j,&hsize,&vsize);
  122.   vst_color(handle,1);
  123.   vsl_color(handle,1);
  124.   vswr_mode(handle,1);
  125.   /* Allocate new RS232 buffer.  calloc() is used because it zeroes the
  126.      memory it allocates */
  127.   rb = calloc(buffer_size,sizeof(char));
  128.   /* If we can't allocate the memory, hang */
  129.   if (rb == NULL)
  130.     for (;;)
  131.       evnt_mesag(msg);
  132.   set_rsbuf();
  133.   for (;;) {
  134.     evnt_mesag(msg);  /* Wait around to be selected */
  135.     if (msg[0] == AC_OPEN)
  136.       acc();
  137.     }
  138. }
  139.  
  140. /*
  141.  * This is the main accessory - it handles messages and i/o.
  142.  */
  143. acc()
  144. {
  145.   int msg[8], d, which;
  146.   int i;
  147.   int a, j;
  148.   int desel = 0; /* Is the window deselected? */
  149.   int dx,dy,dw,dh;
  150.   int stopped = 0; /* Has the window been stopped with ALT-S? */
  151.   long l;
  152.   int p[4];
  153.  
  154.   if (init_window() == 0) return; /*If we can't allocate a window, forget it*/
  155.   align8(&x,&y,&w,&h);
  156.   graf_growbox(24,0,56,16,x,y,w,h);
  157.   wind_open(w_handle,x,y,w,h); /* Open our window */
  158.   wind_get(w_handle,WF_WORKXYWH,&tx,&ty,&tw,&th); /* Get its work area */
  159.   clr(); /* clear it */
  160.   Cauxout(17); /* Send a CTRL-Q, just in case */
  161.   for(;;) {
  162.     /* Release time to the time slicer, but come back every (theoretically)
  163.        0 milliseconds.  Also wait for messages. */
  164.     which = evnt_multi(MU_MESAG|MU_TIMER,0,0,0,0,0,0,0,0,0,0,0,0,0,
  165.                        msg,0,0,&d,&d,&d,&d,&d,&d);
  166.     /* See if our window is still on top */
  167.     wind_get(w_handle,WF_TOP,&a,&j,&j,&j);
  168.     if ((a != w_handle) && (desel == 0)) {   /* Window has been deselected */
  169.       if (stopped != 1) Cauxout(19); /* Send a CTRL-S if the window isn't
  170.                                         stopped */
  171.       desel = 1;
  172.       }
  173.     else
  174.       if ((a == w_handle) && (desel == 1)) { /* Window has been selected */
  175.         if (stopped != 1) Cauxout(17); /* Send a CTRL-Q, if window isn't
  176.                                           stopped */
  177.         desel = 0;
  178.         }
  179.     /* If there are no messages and our window is on top, do some i/o */
  180.     if ((which & MU_TIMER) && (a == w_handle)) {
  181.       /* If there's a character waiting at the keyboard, get it */
  182.       if (Bconstat(2)) {
  183.         l = Bconin(2);
  184.         if (l == 2031616L) /* Is it ALT-S? */
  185.           if (stopped == 0) { /* Yes, toggle stopped window */
  186.             Cauxout(19);
  187.             stopped = 1;
  188.             }
  189.           else {
  190.             Cauxout(17);
  191.             stopped = 0;
  192.             }
  193.         Cauxout((int)l); /* Output the character to RS232.  If it was ALT-S,
  194.                             a 0 will be output */
  195.         }
  196.       /* If there's a character waiting at the RS232 port, and the
  197.          window is not stopped, output it. */
  198.       if (Cauxis() && (stopped == 0)) {
  199.         i = Cauxin();
  200.         MOUSE_OFF; /* turn off mouse */
  201.         if (cursor) { /* erase cursor */
  202.           p[0] = (nx + (hsize / 2));
  203.           p[1] = (ny - 1);
  204.           p[2] = p[0];
  205.           p[3] = ((ny + vsize) + 1);
  206.           vswr_mode(handle,3);
  207.           v_pline(handle,2,p);
  208.           vswr_mode(handle,1);
  209.           }
  210.         outchar(i); /* output the character */
  211.         if (cursor) { /* draw the cursor */
  212.           p[0] = (nx + (hsize / 2));
  213.           p[1] = (ny - 1);
  214.           p[2] = p[0];
  215.           p[3] = ((ny + vsize) + 1);
  216.           vswr_mode(handle,3);
  217.           v_pline(handle,2,p);
  218.           vswr_mode(handle,1);
  219.           }
  220.         MOUSE_ON; /* turn on the mouse */
  221.         }
  222.       }
  223.     if (which & MU_MESAG) {
  224.       switch(msg[0]) {
  225.         case WM_REDRAW : redraw(); /* redraw window and reset character */
  226.                          nx=tx;    /* coordinates                       */
  227.                          ny=ty;
  228.                          break;
  229.         case WM_NEWTOP : /* put appropriate window on top */
  230.         case WM_TOPPED : wind_set(w_handle,WF_TOP,msg[3],0,0,0);
  231.                          clr();
  232.                          break;
  233.         case AC_OPEN   : do_menu(); /* Let the user select a command */
  234.                          break;
  235.         case AC_CLOSE  : return; /* Our window has been closed and deleted */
  236.                          break;  /* for us.                                */
  237.         case WM_CLOSED : wind_close(w_handle); /*